package com.javaweb.controller;

/**
 * @desc:
 * @author: jzy
 * @date: 2024/10/22 23:01
 */

import com.javaweb.common.ResponseResult;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.UserConditionDto;
import com.javaweb.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * todo: 用户管理模块
 */
@RestController
@RequestMapping("/blog-api/v0.1/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public ResponseResult<List<User>> list(@RequestBody UserConditionDto userCond) {
        // 调用 Service 层方法，根据查询条件查询用户列表
        List<User> userList = userService.findUsersByCondition(userCond);
        // System.out.println(userList);
        // 返回结果，封装在ResponseResult中
        return ResponseResult.ok(userList, "查询成功");
    }
    @PostMapping("/add")
    public ResponseResult add(@RequestBody UserDto userDto) {
        // 添加新用户
        userService.addUser(userDto);
        return ResponseResult.ok("加入成功");
    }

    @PostMapping("/password/change")
    public ResponseResult<Void> changePassword(@RequestParam String userId, @RequestParam String newPassword) {
        // 修改用户密码
        userService.changePassword(userId, newPassword);
        return ResponseResult.ok(null);
    }
}
